package com.rybbaby.tss.front.tssMemberJoinPakcage.service.imp;



import com.google.common.collect.ImmutableMap;
import com.rybbaby.tss.core.utils.StringUtils;
import com.rybbaby.tss.core.web.service.imp.BaseServiceImp;
import com.rybbaby.tss.front.tssAllocation.entity.TssAllocation;
import com.rybbaby.tss.front.tssAllocation.service.ITssAllocationService;
import com.rybbaby.tss.front.tssMemberJoinPakcage.entity.TssMemberJoinPackageBean;
import com.rybbaby.tss.front.tssMemberJoinPakcage.service.ITssMemberJoinPackageService;
import com.rybbaby.tss.front.tssMemberPackage.dao.ITssMemberPackageDao;
import com.rybbaby.tss.front.tssMemberPackage.entity.TssMemberPackage;
import com.rybbaby.tss.front.tssPertainRelation.entity.TssPertainRelation;
import com.rybbaby.tss.front.tssPertainRelation.service.ITssPertainRelationService;
import com.rybbaby.tss.front.tssStudent.dao.ITssStudentDao;
import com.rybbaby.tss.front.tssStudent.entity.TssStudent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.sql.Timestamp;

/**
 * @author 雷春琼
 * @description 会员表数据库操作服务实现
 * @createDate 2017-08-10 上午1:37:58
 */
@Service
@Transactional
public class TssMemberJoinPackageServiceImp extends BaseServiceImp<TssMemberJoinPackageBean> implements ITssMemberJoinPackageService {

    @Autowired
    private ITssStudentDao studentDao;
    @Autowired
    private ITssMemberPackageDao memberPackageDao;
    @Autowired
    private ITssPertainRelationService tssPertainRelationService;
    @Autowired
    private ITssAllocationService tssAllocationService;

    /**
     * 添加会员信息和课时包信息
     *
     * @param immutableMap
     * @return
     */
    @Override
    public boolean save(ImmutableMap immutableMap, TssMemberJoinPackageBean entity) {
        boolean bool = true;
        Serializable pertainRelationId = null;
        Serializable allocationId = null;
        Serializable meemberPakcageId = null;
        Serializable newId = saveStudent(entity);
        if (StringUtils.isNotBlank(immutableMap.get("relationIds"))){
            //新增客户信息和亲属所属关系
            pertainRelationId = savePertainRelation(immutableMap, entity, newId);
            if (pertainRelationId == null ) {
                return !bool;
            }
        }
        //添加客户分配表
        if (StringUtils.isNotBlank(entity.getMemberStaffId())) {
            allocationId = saveAllocation(entity, newId);
            if ( allocationId == null) {
                return !bool;
            }
        }
        //添加会员课时包信息
        if (StringUtils.isNotBlank(entity.getPackageId())) {
            meemberPakcageId = saveMemberPackage(entity, newId);
            if (meemberPakcageId == null) {
                return !bool;
            }
        }
        return bool;
    }


    /**
     * 添加会员课时包信息
     *
     * @param entity
     * @param studentId
     * @return
     */
    public Serializable saveMemberPackage(TssMemberJoinPackageBean entity, Serializable studentId) {
        TssMemberPackage memberPackage = new TssMemberPackage();
        memberPackage.setType(entity.getType());
        memberPackage.setMemberId(studentId.toString());
        memberPackage.setCreateTime(new Timestamp(System.currentTimeMillis()));
        memberPackage.setAttendanceDay(entity.getAttendanceDay());
        memberPackage.setContractNumber(entity.getContractNumber());
        memberPackage.setAdminUserId(entity.getAdminUserId());
        memberPackage.setNature(entity.getNature());
        memberPackage.setNowUnitPrice(entity.getNowUnitPrice());
        memberPackage.setPackageId(entity.getPackageId());
        memberPackage.setPresentClassHour(entity.getPresentClassHour());
        memberPackage.setSurplusValidDate(entity.getSurplusValidDate());
        memberPackage.setSumPrice(entity.getSumPrice());
        memberPackage.setRemark(entity.getPackageRemark());
        memberPackage.setPackageStatus(entity.getStatus());
        memberPackage.setEmpAdminUserId(entity.getPakcageStaffId());
        return memberPackageDao.save(memberPackage);
    }

    /**
     * 添加客户分配信息
     *
     * @param entity
     * @param studentId
     * @return
     */
    public Serializable saveAllocation(TssMemberJoinPackageBean entity, Serializable studentId) {
        TssAllocation allocation = new TssAllocation();
        allocation.setAdminUserId(entity.getAdminUserId());
        allocation.setEmpAdminUserId(entity.getMemberStaffId());
        allocation.setStudentId(studentId.toString());
        allocation.setCreateTime(new Timestamp(System.currentTimeMillis()));
        return this.tssAllocationService.save(allocation);
    }

    /**
     * 新增客户信息和亲属所属关系
     *
     * @param immutableMap
     * @param entity
     * @param studentId
     * @return
     */
    public Serializable savePertainRelation(ImmutableMap immutableMap, TssMemberJoinPackageBean entity, Serializable studentId) {
        Serializable pertainRelationId = null;
        Object patriarchName = immutableMap.get("patriarchNames");
        Object affiliationRelation = immutableMap.get("affiliationRelations");
        String[] patriarchNames = (String[]) patriarchName;
        String[] affiliationRelations = (String[]) affiliationRelation;
        for (int i = 0; i < patriarchNames.length; i++) {
            TssPertainRelation tpr = new TssPertainRelation();
            tpr.setStudentId(studentId.toString());
            tpr.setCreateTime(new java.sql.Timestamp(System.currentTimeMillis()));
            tpr.setPatriarchName(patriarchNames[i]);
            tpr.setAffiliationRelation(Integer.parseInt(affiliationRelations[i]));
            pertainRelationId = this.tssPertainRelationService.save(tpr);
        }
        return pertainRelationId;
    }

    /**
     * 添加客户信息
     * @param entity
     * @return
     */
    public Serializable saveStudent(TssMemberJoinPackageBean entity) {
        TssStudent student = new TssStudent();
        if (StringUtils.isBlank(entity.getPackageId())) {
           /* student.setType(0);*/
            student.setIsMember(false);
        } else {
            /*student.setType(entity.getType());*/
            student.setIsMember(true);
        }
        if (StringUtils.isNotBlank(entity.getMemberStaffId())) {
            student.setListStatus(2);
        } else {
            student.setListStatus(1);
        }
        student.setAdminUserId(entity.getAdminUserId());
        student.setCreateTime(new Timestamp(System.currentTimeMillis()));
        student.setAddressCity(entity.getAddressCity());
        student.setAddressRegion(entity.getAddressRegion());
        student.setAddressProvince(entity.getAddressProvince());
        student.setBirthdayDate(entity.getBirthdayDate());
        student.setChannelSource(entity.getChannelSource());
        student.setContactPhone(entity.getContactPhone());
        student.setDemanDegree(entity.getDemanDegree());
        student.setFamilyAddress(entity.getFamilyAddress());
        student.setListStatus(1);
        student.setEmpAdminUserId(entity.getMemberStaffId());
        student.setSex(entity.getSex());
        student.setRemark(entity.getMemberRemark());
        student.setNickName(entity.getNickName());
        student.setName(entity.getName());
        return studentDao.save(student);
    }
}
